#import "Appdelegate.h"
#import "dfhgwhruebvhrheyluggagerecorder.h"
#import "dfhgwhruebvhrheyagonyleisure.h"
#import "dfhgwhruebvhrheycreepvacation.h"
#import "dfhgwhruebvhrheycandidateroutineViewController.h"
#import "dfhgwhruebvhrheyleapoccurrenceViewController.h"
#import "dfhgwhruebvhrheylaserbeforehandViewController.h"
@interface dfhgwhruebvhrheyleapoccurrenceViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *amountLeftConstraint; 
@property (weak, nonatomic) IBOutlet UILabel *amountTextLabel;
@property (weak, nonatomic) IBOutlet UILabel *dateTextLabel;
@property (weak, nonatomic) IBOutlet UILabel *repaymentMethodLabel;
@property (weak, nonatomic) IBOutlet UILabel *accountNoTextLabel;
@property (weak, nonatomic) IBOutlet UILabel *toggleTextLabel;
@property (weak, nonatomic) IBOutlet UIButton *toggleBtn;
@property (weak, nonatomic) IBOutlet UILabel *overdueView;
@property (weak, nonatomic) IBOutlet UIView *detailView;
@property (weak, nonatomic) IBOutlet UIButton *repayBtn;
@property (nonatomic, strong) dfhgwhruebvhrheycreepvacation *placeholder;
@end
@implementation dfhgwhruebvhrheyleapoccurrenceViewController
- (dfhgwhruebvhrheycreepvacation *)placeholder {
	if (_placeholder == nil) {
		_placeholder = [[NSBundle mainBundle] loadNibNamed:@"dfhgwhruebvhrheycreepvacation" owner:nil options:nil].firstObject;
	}
	return _placeholder;
}
- (void)viewDidLoad {
    [super viewDidLoad];
	[self loadUI];
}
- (void)loadUI {
	self.view.backgroundColor = [UIColor clearColor];
	self.placeholder.hidden = YES;
	[self.view addSubview:self.placeholder];
	AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
	if (delegate.status == AFNetworkReachabilityStatusNotReachable) {
		[self configurePlaceHolder:1];
	}
}
- (IBAction)repayAction {
	NSLog(@"还款");
	dfhgwhruebvhrheylaserbeforehandViewController *repay = [[dfhgwhruebvhrheylaserbeforehandViewController alloc] init];
	repay.hidesBottomBarWhenPushed = YES;
	[self.navigationController pushViewController:repay animated:YES];
}
- (IBAction)toggleAction:(UITapGestureRecognizer *)sender {
	self.toggleBtn.selected = !self.toggleBtn.selected;
	self.detailView.hidden = !self.toggleBtn.selected;
	self.toggleTextLabel.text = self.toggleBtn.selected ? @"Put away" : @"More";
}
- (void)bindData:(dfhgwhruebvhrheyagonyleisure *)model {
	self.dateTextLabel.text = model.repayment_date;
	self.amountTextLabel.text = [NSString stringWithFormat:@"PHP %@", model.money.dhgfywegwygeydwlocalitylearned];
    if (model.over_days == 0) {
        self.overdueView.hidden = YES;
        self.amountLeftConstraint.constant = 12;
        self.repayBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
    }else {
        self.overdueView.hidden = NO;
        self.amountLeftConstraint.constant = 76;
        self.repayBtn.backgroundColor = text_color5.dhgfywegwygeydwlearneddesignate;
    }
	self.repaymentMethodLabel.text = model.pay_name;
	self.accountNoTextLabel.text = [model.account_no stringByReplacingCharactersInRange:NSMakeRange(0, model.account_no.length - 4) withString:@"****"];
}
- (void)configurePlaceHolder:(int)type {
	self.placeholder.hidden = NO;
	__weak typeof(self) weakSelf = self;
	if (type == 1) {
		self.placeholder.iconName = @"net_unavaiable";
		self.placeholder.placeholder = @"Content retrieval failed, click to reload.";
		[self.placeholder placeholder:@"Reload" andAction:^{
			__strong typeof(weakSelf) strongSelf = weakSelf;
			[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
			[strongSelf loadData:^{
				[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
			}];
		}];
	}else {
		self.placeholder.iconName = @"no_data";
		self.placeholder.placeholder = @"No historical information";
		[self.placeholder placeholder:@"Get Loan" andAction:^{
			__strong typeof(weakSelf) strongSelf = weakSelf;
			dfhgwhruebvhrheycandidateroutineViewController *tabBar = (dfhgwhruebvhrheycandidateroutineViewController *)[dfhgwhruebvhrheyoptionsaving.keyWindow rootViewController];
			tabBar.selectedIndex = 0;
			[strongSelf.navigationController popToRootViewControllerAnimated:YES];
		}];
	}
}
- (void)loadData:(void(^)(void))result {
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:pendingApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		strongSelf.placeholder.hidden = YES;
		dfhgwhruebvhrheyagonyleisure *model = [dfhgwhruebvhrheyagonyleisure yy_modelWithJSON:json];
		if (model.money.length == 0) {
			[strongSelf configurePlaceHolder:0];
		}else {
			if (model.code == 200) {
				[strongSelf bindData:model];
			}else {
				[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
			}
		}
		if (result) {
			result();
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[strongSelf configurePlaceHolder:1];
		if (result) {
			result();
		}
	}];
}
- (void)viewDidLayoutSubviews {
	[super viewDidLayoutSubviews];
	[self.placeholder mas_makeConstraints:^(MASConstraintMaker *make) {
		make.edges.equalTo(self.view);
	}];
}
@end
